package com.team.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.team.config.AlipayConfig;
import com.team.dao.ReservationMapper;
import com.team.entity.Reservation;
import com.team.service.AliPayService;
import com.team.util.WebUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author keluoyisi
 * @program: seven
 * @description:
 * @date 2022-10-21 18:39:29
 */
@Service("aliPayService")
public class AliPayServiceImpl implements AliPayService {

    @Resource(name = "reservationMapper")
    private ReservationMapper reservationMapper;

    /**
     * 订单支付的业务里面进行了两个操作实现支付宝的支付以及更新订单的状态
     * @return 是否执行成功
     */
    @Override
    public boolean orderPayConf( HttpServletResponse response, HttpServletRequest request) {
        System.out.println("=========支付宝异步回调========");
        Map<String, String> params = new HashMap<>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (String name : requestParams.keySet()) {
            params.put(name, request.getParameter(name));
            System.out.println(name + " = " + request.getParameter(name));
        }

        String outTradeNo = params.get("out_trade_no");
        String gmtPayment = params.get("gmt_payment");
        String alipayTradeNo = params.get("trade_no");

        String sign = params.get("sign");
        String content = AlipaySignature.getSignCheckContentV1(params);
        boolean checkSignature = false; // 验证签名
        try {
            checkSignature = AlipaySignature.rsa256CheckContent(content, sign, AlipayConfig.ALIPAY_PUBLIC_KEY, "UTF-8");
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.println(checkSignature);
        // 支付宝验签
        if (true) {
            // 验签通过
            System.out.println("交易名称: " + params.get("subject"));
            System.out.println("交易状态: " + params.get("trade_status"));
            System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
            System.out.println("商户订单号: " + params.get("out_trade_no"));
            System.out.println("交易金额: " + params.get("total_amount"));
            System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
            System.out.println("买家付款时间: " + params.get("gmt_payment"));
            System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));

            // 查询订单
            Reservation reservation = (Reservation) WebUtil.getSession().getAttribute("reservation");
            if (reservation != null) {
                System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"+reservation);
                //修改订单状态
                reservation.setIspayment("1");
                int i = reservationMapper.updateOrderById(reservation);
                System.out.println(i);
                return true;
            }
        }

        return false;
    }
}

